package cn.iocoder.yudao.module.erp.service.purchaseplan.purchaseservicedetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseservicedetail.PurchaseServiceDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseservicedetail.PurchaseServiceDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.purchaseservicedetail.PurchaseServiceDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.purchaseservicedetail.PurchaseServiceDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_SERVICE_DETAIL_NOT_EXISTS;

/**
 * 服务采购详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurchaseServiceDetailServiceImpl implements PurchaseServiceDetailService {

    @Resource
    private PurchaseServiceDetailMapper purchaseServiceDetailMapper;

    @Override
    public void createPurchaseServiceDetailBatch(List<PurchaseServiceDetailSaveReqVO> createReqVO) {
        // 插入
        List<PurchaseServiceDetailDO> purchaseServiceDetail = BeanUtils.toBean(createReqVO, PurchaseServiceDetailDO.class);
        purchaseServiceDetailMapper.insertBatch(purchaseServiceDetail);
    }

    @Override
    public void updatePurchaseServiceDetail(PurchaseServiceDetailSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseServiceDetailExists(updateReqVO.getId());
        // 更新
        PurchaseServiceDetailDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseServiceDetailDO.class);
        purchaseServiceDetailMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseServiceDetail(Long id) {
        // 校验存在
        validatePurchaseServiceDetailExists(id);
        // 删除
        purchaseServiceDetailMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseServiceDetailListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseServiceDetailExists(ids);
        // 删除
        purchaseServiceDetailMapper.deleteByIds(ids);
        }

    private void validatePurchaseServiceDetailExists(List<Long> ids) {
        List<PurchaseServiceDetailDO> list = purchaseServiceDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_SERVICE_DETAIL_NOT_EXISTS);
        }
    }

    private void validatePurchaseServiceDetailExists(Long id) {
        if (purchaseServiceDetailMapper.selectById(id) == null) {
            throw exception(PURCHASE_SERVICE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseServiceDetailDO getPurchaseServiceDetail(Long id) {
        return purchaseServiceDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseServiceDetailDO> getPurchaseServiceDetailPage(PurchaseServiceDetailPageReqVO pageReqVO) {
        return purchaseServiceDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        purchaseServiceDetailMapper.deleteByPlanId(planId);
    }

    @Override
    public List<PurchaseServiceDetailDO> getByPlanId(Long planId) {
        return purchaseServiceDetailMapper.selectByPlanId(planId);
    }


}